<?php
$dsn = 'pgsql:dbname=publishing_gambettola;host=10.77.98.22';
$user = 'postgres';
$password = 'postgres';
// these variables must be set to match the actual connection
try {
	$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
	die('Connection failed: ' . $e->getMessage());
}
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if($_GET['id']){
	
	$sql="select encode(buffer,'base64') as data from fstream where pk_id=?";
	$sqh=$dbh->prepare($sql);
	$sqh->execute(array($_GET['id']));
	$results=$sqh->fetchAll(PDO::FETCH_NUM);

	$data = $results[0][0];
	
	header('Content-Type: application/binary');
	header('Content-Disposition: attachment; filename="'.$_GET['id'].'.pdf"');
	echo base64_decode($data);
}

?>